//package com.example.cert;
//
//import org.bouncycastle.asn1.x500.X500Name;
//import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
//import org.bouncycastle.cert.X509CertificateHolder;
//import org.bouncycastle.cert.X509v3CertificateBuilder;
//import org.bouncycastle.operator.ContentSigner;
//import org.bouncycastle.operator.DefaultDigestAlgorithmIdentifierFinder;
//import org.bouncycastle.operator.DefaultSignatureAlgorithmIdentifierFinder;
//import org.bouncycastle.operator.bc.BcRSAContentSignerBuilder;
//import org.bouncycastle.operator.bc.BcRSAContentVerifierProviderBuilder;
//
//import java.io.ByteArrayInputStream;
//import java.io.FileOutputStream;
//import java.math.BigInteger;
//import java.security.KeyPair;
//import java.security.KeyPairGenerator;
//import java.security.KeyStore;
//import java.security.NoSuchAlgorithmException;
//import java.security.PrivateKey;
//import java.security.Security;
//import java.security.cert.Certificate;
//import java.security.cert.CertificateFactory;
//import java.security.cert.X509Certificate;
//import java.util.Date;
//
//public class GenerateFakeCertificate {
//
//    public static void main(String[] args) {
//        try {
//            Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
//
//            // 生成RSA密钥对
//            KeyPair keyPair = generateRSAKeyPair();
//
//            // 创建自签名证书
//            X509Certificate certificate = generateSelfSignedCertificate(keyPair);
//
//            // 保存证书和私钥为P12文件
//            saveCertificateAndPrivateKeyAsP12(certificate, keyPair.getPrivate(), "fake_ssl_certificate.p12", "p12_password");
//
//            System.out.println("Fake SSL certificate and private key generated successfully!");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    private static KeyPair generateRSAKeyPair() throws NoSuchAlgorithmException {
//        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
//        keyPairGenerator.initialize(2048);
//        return keyPairGenerator.generateKeyPair();
//    }
//
//    private static X509Certificate generateSelfSignedCertificate(KeyPair keyPair) throws Exception {
//        // 证书有效期
//        Date startDate = new Date();
//        Date endDate = new Date(startDate.getTime() + 365 * 24 * 60 * 60 * 1000); // 1 year
//
//        // 证书序列号
//        BigInteger serialNumber = BigInteger.valueOf(System.currentTimeMillis());
//
//        // 创建自签名证书
//        X500Name issuerName = new X500Name("CN=Fake Certificate Issuer");
//        X500Name subjectName = new X500Name("CN=Fake Certificate Subject");
//
//        X509v3CertificateBuilder certificateBuilder = new X509v3CertificateBuilder(
//                issuerName,
//                serialNumber,
//                startDate,
//                endDate,
//                subjectName,
//                SubjectPublicKeyInfo.getInstance(keyPair.getPublic().getEncoded()
//                )
//        );
//
//        ContentSigner signer = new BcRSAContentSignerBuilder(
//                new DefaultSignatureAlgorithmIdentifierFinder().find("SHA256withRSA"),
//                new DefaultDigestAlgorithmIdentifierFinder().find("SHA256"),
//                new BcRSAContentVerifierProviderBuilder(new DefaultDigestAlgorithmIdentifierFinder().find("SHA256"))
//                        .build(keyPair.getPublic()))
//                .build(keyPair.getPrivate());
//
//        X509CertificateHolder certificateHolder = certificateBuilder.build(signer);
//
//        // 转换为X509证书对象
//        CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
//        ByteArrayInputStream inputStream = new ByteArrayInputStream(certificateHolder.toASN1Structure().getEncoded());
//        return (X509Certificate) certificateFactory.generateCertificate(inputStream);
//    }
//
//    private static void saveCertificateAndPrivateKeyAsP12(X509Certificate certificate, PrivateKey privateKey, String filePath, String password) throws Exception {
//        KeyStore keyStore = KeyStore.getInstance("PKCS12");
//        keyStore.load(null, null);
//
//        keyStore.setKeyEntry("alias", privateKey, password.toCharArray(), new Certificate[]{certificate});
//
//        FileOutputStream outputStream = new FileOutputStream(filePath);
//        keyStore.store(outputStream, password.toCharArray());
//        outputStream.close();
//    }
//}
